// pages/index/component/foodAd/foodAd.js
import {
  list
} from '../../../../api/ad'
const app = getApp()
import util from '../../../../utils/util'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: {
      type: Object
    }
  },

  lifetimes: {
    attached() {
      var _this = this
      if (this.data.info.tagids != null) {
        this.data.tagIds = this.data.info.tagids.split(',')
        for (var i = 0; i < this.data.tagIds.length; i++) {
          this.data.tagIds[i] = parseInt(this.data.tagIds[i])
        }
      }
      this.setData({
        tagIds: this.data.tagIds
      })
      this.loadAd(()=>{
        wx.getImageInfo({
          src: this.data.adList.thumbnail,
          success: function (res) {
            _this.data.imageHeight = res.height + 'px'
            _this.setData({
              imageHeight: _this.data.imageHeight
            })
          }
        })
      })
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    imageHeight: '',
    adList: {},
    tagIds: [],
  },


  /**
   * 组件的方法列表
   */
  methods: {
    toDetail(data) {
      console.info("data.currentTarget.dataset.type-----------------------------")
      console.info(data.currentTarget)
      if (data.currentTarget.dataset.type == 0) {
        wx.navigateTo({
          url: '/pages/shop/productDetail/productDetail?productId=' + data.currentTarget.dataset.id
        })
      } else if (data.currentTarget.dataset.type == 2) {
        wx.navigateTo({
          url: '/pages/hotspot/hotspot/index?id=' + data.currentTarget.dataset.id,
        })
      } else if (data.currentTarget.dataset.type == 3) {
        wx.navigateTo({
          url: '/pages/discovery/details/details?id=' + data.currentTarget.dataset.id
        })
      } else if (data.currentTarget.dataset.type == 4) {
        wx.navigateTo({
          url: '/pages/travel/travel/index?scenicSpotId=' + data.currentTarget.dataset.id
        })
      } else if (data.currentTarget.dataset.type == 5) {
        wx.navigateTo({
          url: '/pages/shop/shopDetail/index?shopId=' + data.currentTarget.dataset.id + '&mchId=' + app.globalData.mchId
        })
      } else if (data.currentTarget.dataset.type == 6) {
        wx.navigateTo({
          url: '/pages/cook/kitchenBrigade/kitchenBrigade?id=' + data.currentTarget.dataset.id + '&mchId=' + app.globalData.mchId
        })
      } else if (data.currentTarget.dataset.type == 7) {
        wx.navigateTo({
          url: '/pages/shop/shopDetail/index?shopId=' + data.currentTarget.dataset.id + '&mchId=' + app.globalData.mchId
        })
      } else if (data.currentTarget.dataset.type == 8) {
        wx.navigateTo({
          url: '/pages/coupon/coupon',
        })
      }else if (data.currentTarget.dataset.type == 9) {
        wx.navigateTo({
          url: '/pages/index/wxPage/wxPage?id='+data.currentTarget.dataset.id 
        })
      } else if (data.currentTarget.dataset.type == 12) {

      }
    },
    loadAd(callback) {
      list({
        tagIds: this.data.tagIds,
        pageSize:1,
        mchId: app.globalData.mchId,
      }).then(res => {
        console.info("loadAd-----------------")
        console.info(res)
        if(!util.isNull(res.data.data)){
          res.data.data[0].thumbnail=util.thumbnail_cover(res.data.data[0].thumbnail,670)
        }
        this.data.adList = res.data.data[0]

        this.setData({
          adList: this.data.adList
        })
        if(callback){
          callback()
        }
      })
    }
  }
})